#  playlist.py - Playlist abstraction module
#  Copyright (C) 2005-2007 Sham Chukoury <eleusis@xmms.org>
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2.1 of the License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.

from sepal import Sepal
from Chalyx.base import SigProxyObj, ProxiedSignal

class Playlist(Sepal, SigProxyObj):
	def __init__(self, service, library, playlistName = None):
		def __set_pls_name(value):
			self.playlistName = value
			self.emit("name_changed", self.playlistName)
		def __on_connect(obj, connected, udata):
			if connected:
				self.service.playlist_current_active(__set_pls_name)
		Sepal.__init__(self, service)
		SigProxyObj.__init__(self)
		self.playlistName = playlistName
		if self.playlistName is None:
			self.service.connect("service_connected", __on_connect)
		self.library = library
		self.signals = {
			"playlist_position": ProxiedSignal("playlist_position", self.service, "pl_pos", self._curpos, None),
			"playlist_changed": ProxiedSignal("playlist_changed", self.service, "pl_change", self._change, None),
			"playlist_repeat": ProxiedSignal("playlist_repeat", self.service, "pl_repeat_change", self._rpt, None),
			"playlist_loaded": ProxiedSignal("playlist_loaded", self.service, "pl_load", self._loaded, None),
			"name_changed": []
		}
		self.pos = -1

	def next(self, *args):
		self.service.playlist_next()

	def prev(self, *args):
		self.service.playlist_prev()

	def jump(self, pos):
		self.service.playlist_jump(pos)

	def play_pos(self, pos):
		self.service.playlist_play_pos(pos)
	
	# TODO: return playlist instances?
	def list(self, cb):
		self.service.playlist_list(cb)
	
	def load(self, playlist):
		def __on_loaded(name):
			self.emit("name_changed", name)
		self.service.playlist_load(playlist, __on_loaded)
	
	def list_entries(self, retCB):
		def __idlist_cb(idlist):
			tmplist = []
			asyncState = {"resolved": 0}
			def __info_cb(data, pos):
				tmplist.append((pos, data))
				asyncState["resolved"] += 1
				if asyncState["resolved"] == len(idlist):
					tmplist.sort()
					retCB([x[1] for x in tmplist])
			map(lambda x: self.library.item_info(x[1], __info_cb, x[0]), enumerate(idlist))
		self.service.playlist_list(__idlist_cb, self.playlistName)
	
	def list_entries_id(self, cb):
		self.service.playlist_list_entries(cb)

	def current_pos(self, cb):
		def __pos_cb(pos):
			self.pos = int(pos)
			cb(self, pos)
		self.service.playlist_current_pos(__pos_cb)

	def remove(self, pos):
		self.service.playlist_remove(pos)

	def add(self, url):
		self.service.playlist_add(url)
	
	def add_url(self, url):
		print url
		self.service.playlist_add_url(url)

	def add_from_library(self, id):
		self.service.playlist_add_from_library(id)

	def insert(self, pos, url):
		self.service.playlist_insert(pos, url)

	def insert_from_library(self, pos, id):
		self.service.playlist_insert_from_library(pos, id)
	
	def move(self, fromID, toID):
		self.service.playlist_move(fromID, toID)
	
	def shuffle(self, *args):
		self.service.playlist_shuffle()
	
	def get_repeat(self, cb, udata=None):
		def __rpt_cb(rpt):
			if udata:
				cb(rpt, udata)
			else:
				cb(rpt)
		self.service.playlist_get_repeat(__rpt_cb)
	
	def set_repeat(self, repeat):
		self.service.playlist_set_repeat(repeat)
	
	def _loaded(self, obj, sigdata, udata):
		self.playlistName = sigdata
		self.emit("playlist_loaded", sigdata)
	
	def _curpos(self, obj, sigdata, udata):
		self.pos = int(sigdata)
		self.emit("playlist_position", self.pos)

	def _change(self, obj, sigdata, udata):
		self.emit("playlist_changed", sigdata)
	
	def _rpt(self, obj, sigdata, udata=None):
		self.emit("playlist_repeat", sigdata)
